buildType=$1

basepath="/Users/weli/WorkSpace"
shPath=$(dirname ${BASH_SOURCE[0]})
versionName=$(cat $shPath/version.txt)
cd /Users/weli/WorkSpace
frameworkPath="${basepath}/Web/unity_framework"
frameworkVersionPath="${basepath}/Web/unity_framework.txt"
frameworkBuildPath="${basepath}/Web/unity_framework/output"
iosProjectPath="${basepath}/mate-client-ios/MateiOS"
iosArchivePath="${basepath}/Web/archive"
exportIpaPath="${basepath}/Web/mate_ipa"
zipFrameworkPath="${basepath}/Web/UnityFramework.framework.zip"
IPV4=$(/sbin/ifconfig en0 | grep 'inet ' | awk '{print $2}')

# local framewrok version
if [ -f $frameworkVersionPath ]
then
    frameworkurl0=$(cat $frameworkVersionPath)
fi

# remote framework url
frameworkurl=$(curl -s https://wlops.obs.cn-north-4.myhuaweicloud.com/mateurp_ios/unity_framework_$versionName.txt)

# build unity framework
buildframework()
{
    if [ "$buildType" == "" ]
    then
        buildType="Release"
    fi
    echo "start build unity framework..."
    if [ -d $frameworkBuildPath ]
    then
        rm -rf $frameworkBuildPath
    fi
    mkdir $frameworkBuildPath
    mkdir $frameworkBuildPath/Data
    cp -r $frameworkPath/Data/* $frameworkBuildPath/Data
    xcodebuild clean -project $frameworkPath/Unity-iPhone.xcodeproj -target UnityFramework || exit 1
    xcodebuild build -project $frameworkPath/Unity-iPhone.xcodeproj -target UnityFramework || exit 1
    cp -r $frameworkPath/build/Release-iphoneos/UnityFramework.framework/* $frameworkBuildPath || exit 1
    echo "finish build unity framework!"
}

# 压缩zip
compressFramework()
{
	if [ -f $zipFrameworkPath ]
	then
		rm -f $zipFrameworkPath
	fi
	echo "start compress UnityFramework..."
	cd $frameworkBuildPath
    zip -r $zipFrameworkPath *
	echo "finish compress UnityFramework..."
}

# pull ios framework
pullIosFramework()
{
    cd $iosProjectPath
    # Update Code
    echo "start pull ios project..."
    git checkout -f dev
    git fetch origin || exit 1
    git reset --hard "origin/dev" || exit 1
	echo "finish pull ios project!"
}

# export ios to ipa
buildIpa()
{
	echo "start build ipa..."
    if [ -d $iosProjectPath/UnityFramework.framework ]
    then
        rm -rf $iosProjectPath/UnityFramework.framework
    fi
    mkdir $iosProjectPath/UnityFramework.framework
    cp -rf $frameworkBuildPath/*  $iosProjectPath/UnityFramework.framework
    
    if [ -d $iosArchivePath.xcarchive ]
    then
        rm -rf $iosArchivePath.xcarchive
    fi
    
    #mkdir $iosArchivePath
    cd $iosProjectPath
    xcodebuild clean -workspace $iosProjectPath/MateiOS.xcworkspace -configuration $buildType -destination 'generic/platform=iOS'
    xcodebuild archive -workspace $iosProjectPath/MateiOS.xcworkspace -scheme "MateiOS" -configuration $buildType -archivePath $iosArchivePath -destination 'generic/platform=iOS'
	echo "finish build ipa"
}

# export ipa frame archive
exportIpa()
{
    if [ -d $exportIpaPath ]
	then
		rm -rf $exportIpaPath
	fi
	echo "start export ipa..."
	xcodebuild -exportArchive -archivePath $iosArchivePath.xcarchive -exportPath $exportIpaPath -exportOptionsPlist ${basepath}/GoogleService-Info.plist
	echo "finish export ipa!"
}

# upload ipa
uploadIpa()
{
	echo "start upload ipa..."
	dateString=$(date "+%Y%m%d%H%M")
	upload_ipa=mate_test_Mate_${versionName}_${dateString}.ipa
        cd $exportIpaPath
		cp -f MateiOS.ipa $upload_ipa
	
	if [ ! -f $upload_ipa ]
	then
		echo "no ipa file created!"
		exit 1
	fi
        
	upload_url="http://appstore-admin.etouch.cn/app-store-server/api/app/pkg/latest/upload"
	result=`curl --location --request POST $upload_url --header 'Content-Type: multipart/form-data' --form "file=@$upload_ipa" --form "ver_desc=ios测试包"`
	echo "finish upload ipa:"$result
}

# notice to dingtalk
sendNotice(){
    json="{\"text\":{\"content\":\"IOS打包测试Ipa，并上传微鲤市场完成,下载地址 https://appstore.wekoi.cn/#/appDetail/116  UnityFramework临时下载地址：http://${IPV4}/unityframework_${versionName}.zip\"},\"msgtype\":\"text\"}"
    echo "send notice $json"
    curl -H "Content-Type: application/json" -X POST -d "$json" https://oapi.dingtalk.com/robot/send?access_token=1dd9ef311951334ba9e001f194f78c99ee8b74fff2c56b5f899c1727302f60b8
}

if [ "$frameworkurl" != "$frameworkurl0" ]
then
    echo "start download unity-project:"$frameworkurl
    # delete zip path
    if [ -f $frameworkPath.zip ]
    then
        rm -f $frameworkPath.zip
        echo "remove old framework zip"
    fi
    
    # delete framework folder
    if [ -d $frameworkPath ]
    then
        rm -rf $frameworkPath
        echo "remove old framework dir"
    fi
	
    curl -o ${frameworkPath}.zip $frameworkurl
    unzip ${frameworkPath}.zip -d ${frameworkPath}
    
    if [ ! -d $frameworkPath ]
    then
        echo "unity framework not download ok!"
        exit 1
    fi
    buildframework
	compressFramework
    echo ${frameworkurl}>${frameworkVersionPath}
	echo "finish download unity-project!"
fi


pullIosFramework
buildIpa
exportIpa
uploadIpa
sendNotice


